package cn.wolfcode.luowowo.controller;


import cn.wolfcode.luowowo.annotation.CurrentUser;
import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.domain.User;
import cn.wolfcode.luowowo.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.service.IUserService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 用户请求控制器
 */
@RestController
@RequestMapping("users")
public class UserController {
    //注入业务层
    @Autowired
    private IUserService userService;
    @Autowired
    private IStrategyStatisVORedisService redisService;

    //注入配置文件中短信api的url样式和appkey
    @Value("${sms.url}")
    private String url;
    @Value("${sms.appkey}")
    private String appkey;

    @GetMapping("/get")
    public Object get(String id){
        return userService.get(id);

    }
    //验证手机号
    @GetMapping("/checkPhone")
    public boolean checkPhone(String phone){
        return userService.checkPhone(phone);
    }
    //发送验证码
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userService.sendVerifyCode(phone , url ,appkey);
        //返回发送成功的结果
        return JsonResult.success();
    }

    /**
     *通过f12发现注册提交的参数有这5个
     *
     * @return
     */
    @PostMapping("/regist")
    public Object regist(String phone , String nickname , String password , String rpassword , String verifyCode){
        userService.regist(phone,nickname,password,rpassword,verifyCode);
        //返回发送成功的结果
        return JsonResult.success();
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Object login(String username , String password){
        Map<String, String> loginData = userService.login(username, password);
        //返回登录成功的结果
        return JsonResult.success(loginData);
    }

    /**
     * 获取登录用户信息
     * @RequestHeader根据
     * @param token 请求头中的token
     * @return
     */
    @GetMapping("/currentUser")
    public Object currentUser(@RequestHeader("token")String token){
        User currentUser = userService.getCurrentUser(token);
        //返回用户信息
        return JsonResult.success(currentUser);
    }
    //获取用户攻略的收藏集合
    @GetMapping("/strategies/favor")
    public Object favor(String sid , @CurrentUser User user){
        List<String> list = redisService.getFavors(user.getId());

        return JsonResult.success(list);
    }
}
